package com.frogs.ucenter.v1.request;

import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import com.frogs.framework.util.StringUtil;
import com.frogs.framework.web.HttpRequestInfo;
import com.frogs.system.util.ApiUrlParamEnum;
import com.frogs.system.util.ApplicablePlatformEnum;
import com.frogs.system.web.api.request.Request;
import com.frogs.system.web.exception.RequestIllegalException;
import com.frogs.ucenter.util.CpsConstant;

/**
 * 注册用请求参数类
 *
 * @author lijian
 * @version 2014年4月8日 - 下午8:06:03
 */
public class UserAccountInfoAddRequest extends Request {

	/**
	 * 用户名/邮箱/手机号码
	 */
	private String username;
	
	/**
	 * 昵称
	 */
	private String nickname;
	
	/**
	 * 密码
	 */
	private String passwd;
	
	/**
	 * 重复密码
	 */
	private String confirmPasswd;
	
	/**
	 * 适用范围
	 */
	private int applicablePlatformId;
	
	private String expandCode;
	
	private String ip;
	
	private String realName;
	
	@Override
	public void parse(HttpServletRequest request) {
		
		HttpRequestInfo requestInfo = new HttpRequestInfo(request);
		
		this.username = requestInfo.getParameter(ApiUrlParamEnum.username.getParamName(), "");
		this.nickname = requestInfo.getParameter(ApiUrlParamEnum.nickName.getParamName(), "").trim();
		this.passwd = requestInfo.getParameter(ApiUrlParamEnum.password.getParamName(), "");
		this.confirmPasswd = requestInfo.getParameter(ApiUrlParamEnum.confirmPassword.getParamName(), "");
		this.applicablePlatformId = requestInfo.getIntParameter(ApiUrlParamEnum.applicablePlatformId.getParamName(), ApplicablePlatformEnum.ALL.getId());
		this.expandCode = requestInfo.getParameter(ApiUrlParamEnum.expandCode.getParamName(), requestInfo.getCookieValue(CpsConstant.CPS_SOURCE_NAME, ""));
		this.ip = requestInfo.getParameter(ApiUrlParamEnum.ip.getParamName(), requestInfo.getUserIp());
		this.realName = requestInfo.getParameter(ApiUrlParamEnum.realName.getParamName(), "");
		
		if (StringUtil.isEmpty(username)) {
			throw new RequestIllegalException("用户名，邮箱，手机号码不能都空");
		}
		/*if (StringUtil.isEmail(username) || StringUtil.isMobilePhone(username)) {
			if (StringUtil.isEmpty(nickname)) {
				throw new RequestIllegalException("用户昵称不能为空");
			}
		}*/
		if (!StringUtil.isMobilePhone(username)) {
			if (StringUtil.isEmpty(passwd) || StringUtil.isEmpty(confirmPasswd)) {
				throw new RequestIllegalException("密码不能为空");
			}
		}
		if (!StringUtil.isEmpty(passwd) && !StringUtil.isEmpty(confirmPasswd)) {
			if (passwd.length() < 6 || passwd.length() > 20) {
				throw new RequestIllegalException("密码的长度在6-20位");
			}
			if (confirmPasswd.length() < 6 || confirmPasswd.length() > 20) {
				throw new RequestIllegalException("密码的长度在6-20位");
			}
			if (!Pattern.matches("[a-zA-Z0-9`~!@#$%^&*()+=_|{}':;',//[//].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]+$", passwd)
					|| !Pattern.matches("[a-zA-Z0-9`~!@#$%^&*()+=_|{}':;',//[//]\\.<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]+$", confirmPasswd)) {
				throw new RequestIllegalException("密码是由字符，数字和字母组成");
			}
			if (!passwd.equals(confirmPasswd)) {
				throw new RequestIllegalException("两次输入的密码不一致");
			}
		}
		
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getNickname() {
		return nickname;
	}

	public void setNickname(String nickname) {
		this.nickname = nickname;
	}

	public String getPasswd() {
		return passwd;
	}

	public void setPasswd(String passwd) {
		this.passwd = passwd;
	}

	public String getConfirmPasswd() {
		return confirmPasswd;
	}

	public void setConfirmPasswd(String confirmPasswd) {
		this.confirmPasswd = confirmPasswd;
	}

	public int getApplicablePlatformId() {
		return applicablePlatformId;
	}

	public void setApplicablePlatformId(int applicablePlatformId) {
		this.applicablePlatformId = applicablePlatformId;
	}

	public String getExpandCode() {
		return expandCode;
	}

	public void setExpandCode(String expandCode) {
		this.expandCode = expandCode;
	}
	
	public String getIp() {
		return this.ip;
	}
	
	public void setIp(String ip) {
		this.ip = ip;
	}

	public String getRealName() {
		return realName;
	}

	public void setRealName(String realName) {
		this.realName = realName;
	}
}
